package br.com.yahoo.mau_mss.tutorjavafxgolfx.controller;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import br.com.yahoo.mau_mss.tutorjavafxgolfx.TutorJavaFxGolFx;
import br.com.yahoo.mau_mss.tutorjavafxgolfx.model.Produto;

/**
 * <p>Title: CarrinhoApp</p>
 * <p>Description:  </p>
 * <p>Date: Oct 16, 2013, 12:37:25 PM</p>
 * @author Mauricio Soares da Silva (mauricio.soares)
 */
public class CarrinhoApp extends Application {
  private AnchorPane pane;
  private TableView<ItensProperty> tableCarrinho;
  private TableColumn<ItensProperty, String> columnProduto;
  private TableColumn<ItensProperty, Double> columnPreco;
  private Button btExcluirItem, btVoltarVitrine, btConfirmarCompra;
  private static Stage stage;
  private static ObservableList<ItensProperty> listItens;
  private int count;

  /**
   * Create a new instance of <code>CarrinhoApp</code>.
   */
  public CarrinhoApp() {
  }

  @Override
  public void start(Stage stage) throws Exception {
    initComponents();
		initListeners();
		Scene scene = new Scene(pane);
		stage.setScene(scene);
		stage.setTitle("GolFX - Carrinho");
		stage.show();
		initLayout();
		CarrinhoApp.stage = stage;
  }
  
  @SuppressWarnings("unchecked")
  private void initComponents() {
		CarrinhoApp.listItens = FXCollections.observableArrayList();
		this.pane = new AnchorPane();
		this.pane.setPrefSize(800, 600);
		this.pane.setStyle(TutorJavaFxGolFx.STYLE_BACKGROUND);
		this.tableCarrinho = new TableView<>();
		initItens();
		this.tableCarrinho.setItems(listItens);
		this.tableCarrinho.setPrefSize(780, 550);
		this.columnProduto = new TableColumn<>();
		this.columnProduto
				.setCellValueFactory(new PropertyValueFactory<ItensProperty, String>(
						"produto"));
		this.columnProduto.setText("Produto");
		this.columnPreco = new TableColumn<>();
		this.columnPreco
				.setCellValueFactory(new PropertyValueFactory<ItensProperty, Double>(
						"preco"));
		this.columnPreco.setText("Preço");
		this.tableCarrinho.getColumns().addAll(columnProduto, columnPreco);
		this.btExcluirItem = new Button("Excluir Item");
		this.btVoltarVitrine = new Button("Voltar à Vitrine");
		this.btConfirmarCompra = new Button("Confirmar Compra");
		this.pane.getChildren().addAll(this.tableCarrinho, this.btExcluirItem, this.btVoltarVitrine,
				this.btConfirmarCompra);
	}
  
  private void initItens() {
		for (Produto p : VitrineApp.carrinho.getProdutos()) {
      CarrinhoApp.listItens.add(new ItensProperty(p.getProduto(), p.getPreco()));
    }
	}
  
  private void initListeners() {
		this.btExcluirItem.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				VitrineApp.carrinho.removeProduto(new Produto(tableCarrinho
						.getSelectionModel().getSelectedItem().getProduto(),
						tableCarrinho.getSelectionModel().getSelectedItem()
								.getPreco()));
				tableCarrinho.getItems().remove(
						tableCarrinho.getSelectionModel().getSelectedItem());
			}
		});

		this.btVoltarVitrine.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
        CarrinhoApp.getStage().close();
				ItemApp.getStage().close();
			}
		});

		this.btConfirmarCompra.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
        showConfirmacao();
				
			}
		});
	}
  
  private void showConfirmacao() {
    /*try {
      Parent root = FXMLLoader.load(getClass().getResource("fxml/Confirmacao.fxml"));
      Scene scene = new Scene(root);
      Stage stage2 = new Stage(StageStyle.DECORATED);
      stage2.setScene(scene);
      stage2.setTitle("Confirmação");
      stage2.show();
    } catch (IOException ex) {
      Logger.getLogger(CarrinhoApp.class.getName()).log(Level.SEVERE, null, ex);
    }*/
    try {      
      FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Confirmacao.fxml")); 
      Scene scene = new Scene((Pane) loader.load());
      Stage stage2 = new Stage(StageStyle.DECORATED);
      stage2.setScene(scene);
      stage2.setTitle("Confirmação");
      //ConfirmacaoController controller = loader.<ConfirmacaoController>getController();
      stage2.show();
    } catch (IOException ex) {      
      Logger.getLogger(CarrinhoApp.class.getName()).log(Level.SEVERE, null, ex); 
    }
  }
  
  private void initLayout() {
		this.tableCarrinho.setLayoutX(10);
		this.tableCarrinho.setLayoutY(5);
		this.btExcluirItem.setLayoutX(10);
		this.btExcluirItem.setLayoutY(570);
		this.btVoltarVitrine.setLayoutX((this.pane.getWidth() - this.btVoltarVitrine
				.getWidth()) / 2);
		this.btVoltarVitrine.setLayoutY(570);
		this.btConfirmarCompra.setLayoutX(675);
		this.btConfirmarCompra.setLayoutY(570);
	}
  
  public class ItensProperty {
		private SimpleStringProperty produto;
		private SimpleDoubleProperty preco;

		public ItensProperty(String produto, double preco) {
			this.produto = new SimpleStringProperty(produto);
			this.preco = new SimpleDoubleProperty(preco);
		}

		public String getProduto() {
			return this.produto.get();
		}

		public void setProduto(String produto) {
			this.produto.set(produto);
		}

		public double getPreco() {
			return this.preco.get();
		}

		public void setPreco(double preco) {
			this.preco.set(preco);
		}
	}
  
  public static Stage getStage() {
    return stage;
  }

}